package org.slusk.thynwor.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Csv2ResultsSummary {
	
	public static final int GENERATION_COL = 0;
	public static final int FOOD_COL = 3;
	public static final int CHROME_ID_COL = 2;
	public static final String FILENAME = "foodCollectedAll.csv";
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
 		doit(args);
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void doit(String[] args) throws IOException {
		String directory = args[0];
		String runName = args[1];
		String outFileName = args[2];
		
		int numRuns = 20;
		double crossRunBest;
		double bestAverage;
		double stdDev;
		
		String[] bestIndividualArray = new String[numRuns];
		double[] bestFoodCollectedArray = new double[numRuns];
		String[] generationOfBestArray = new String[numRuns];
		
		populateData(directory, runName, bestIndividualArray, bestFoodCollectedArray, generationOfBestArray);
		crossRunBest = findCrossRunBest(bestFoodCollectedArray);
		bestAverage = Util.average(bestFoodCollectedArray);
		stdDev = Util.calculateStdDev(bestFoodCollectedArray, bestAverage);
		
		dumpToFile(outFileName, bestIndividualArray, bestFoodCollectedArray, generationOfBestArray, crossRunBest, bestAverage, stdDev);
	}

	public static void dumpToFile(String outFileName
			, String[] bestIndividualArray
			, double[] bestFoodCollectedArray
			, String[] generationOfBestArray
			, double crossRunBest
			, double bestAverage
			, double stdDev) throws IOException 
	{
		BufferedWriter out = new BufferedWriter(new FileWriter(outFileName));

		
		out.write("Run#,BestIndividual,BestFoodCollected,GenerationOfBest");
		out.write("\n");
		
		// write data
		for (int run = 0; run < bestIndividualArray.length; run++) {
			out.write(run + ",");
			out.write(bestIndividualArray[run] + ",");
			out.write(bestFoodCollectedArray[run] + ",");
			out.write(generationOfBestArray[run] + ",");
			out.write("\n");
		}
		
		out.write("\n");
		out.write("BestFood," + crossRunBest);
		out.write("\n");
		out.write("BestFoodAverage," + bestAverage);
		out.write("\n");
		out.write("Std. Deviation," + stdDev);
		out.write("\n");
		out.close();
		
	}

	public static double findCrossRunBest(double[] bestFoodCollectedArray) {
		double best = -1;
		for (int i = 0; i < bestFoodCollectedArray.length; i++) if (bestFoodCollectedArray[i] > best) best = bestFoodCollectedArray[i];
		return best;
	}

	public static void populateData(String directory, String runName, String[] bestIndividualArray, double[] bestFoodCollectedArray, String[] generationOfBestArray) throws IOException {
		String folderName;
		int run = 1;
		String bestIndividual = null;
		double bestFoodCollected = -1;
		String generationOfBest = null;
		double current;

		// For each folder with run data
		folderName = runName + "_" + run;
		File file = new File(directory+"/"+folderName+"/"+FILENAME);
		while (file.exists()) {		
			SimpleCsv csv = new SimpleCsv(file);		

			for (int row = 1; row < csv.getNumRows(); row++) {
				current = Double.parseDouble(csv.get(row, FOOD_COL));
				if (current > bestFoodCollected) {
					bestFoodCollected = current;
					generationOfBest = csv.get(row, GENERATION_COL);
					bestIndividual = csv.get(row, CHROME_ID_COL);
				}
			}
			bestIndividualArray[run-1] = bestIndividual;
			bestFoodCollectedArray[run-1] = bestFoodCollected;
			generationOfBestArray[run-1] = generationOfBest;
			
			bestFoodCollected = -1;
			run++;
			folderName = runName + "_" + run;
			file = new File(directory+"/"+folderName+"/"+FILENAME);
		}
	}
	
}
